package com.finddeer.bookkeeping.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.finddeer.bookkeeping.Bill;
import com.finddeer.bookkeeping.BillLab;
import com.finddeer.bookkeeping.BillPagerActivity;
import com.finddeer.bookkeeping.R;

import java.util.Collections;
import java.util.List;

/**
 * author    : leaves
 * gitee     : https://gitee.com/LEAVES2018
 * time      : 2019/1/3 11:34 AM
 * function  : 实现搜索功能的Fragment。接收用户搜索内容，通过查找子串的形式获得搜索结果，返回最新的一条搜索结果。
 */
public class SearchFragment extends Fragment {

    EditText mSearchEditText;
    Button mClearButton;
    Button mConfirmButton;

    Bill mBill;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search, container, false);

        mSearchEditText = view.findViewById(R.id.search_edit_text);
        mSearchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mClearButton = view.findViewById(R.id.clear_button);
        mClearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSearchEditText.setText("");
            }
        });

        mConfirmButton = view.findViewById(R.id.confirm_button);
        mConfirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search(mSearchEditText.getText().toString());
            }
        });

        return view;
    }

    public void search(String string) {
        BillLab billLab = BillLab.get(getActivity());
        List<Bill> bills = billLab.getBills();

        Collections.sort(bills);
        Collections.reverse(bills);
        for (int i = 0; i < bills.size(); i++) {
            mBill = bills.get(i);
            String billString = mBill.getTitle() + mBill.getLocale();
            if (billString.indexOf(string) == -1) {
                continue;
            }   //  如果输入字符并非该bill的title的子串
            Intent intent = BillPagerActivity.newIntent(getActivity(), bills.get(i).getId());
            startActivity(intent);
            return;
        }
        Toast.makeText(getActivity(), "找不到该结果", Toast.LENGTH_SHORT).show();
    }
}
